2020-2021 Sunseeker Telemetry and Lighting System
eusci_b_i2c_ex5_masterMultipleSlave.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 #include "driverlib.h"
33 
34 //******************************************************************************
35 // MSP430F5xx_6xx Demo - USCI_B0 I2C Master TX bytes to Multiple Slaves
36 //
37 // Description: This demo connects two MSP430's via the I2C bus.
38 // The master transmits to 4 different I2C slave addresses 0x0A,0x0B,0x0C&0x0D.
39 // Each slave address has a specific related data in the array TXData[].
40 // At the end of four I2C transactions the slave address rolls over and begins
41 // again at 0x0A.
42 // ACLK = n/a, MCLK = SMCLK = BRCLK = default DCO = ~1.045MHz
43 // Use with MSP430FR57xx_uscib0_i2c_MultiSlave.c
44 //
45 // /|\ /|\
46 // MSP430F67791A 10k 10k MSP430F67791A
47 // slave | | master
48 // ----------------- | | -----------------
49 // -|XIN P2.6/UCB0SDA|<-|----+->|P2.6/UCB0SDA XIN|-
50 // | | | | |
51 // -|XOUT | | | XOUT|-
52 // | P2.5/UCB0SCL|<-+------>|P2.5/UCB0SCL |
53 // | | | |
54 //
55 //******************************************************************************
56 
57 
58 uint8_t TXData[]= {0xA1,0xB1,0xC1,0xD1};// Pointer to TX data
59 uint8_t SlaveAddress[]= {0x0A,0x0B,0x0C,0x0D};
60 uint8_t TXByteCtr;
61 uint8_t SlaveFlag = 0;
62 
63 void main(void)
64 {
65  WDT_A_hold(WDT_A_BASE);
66 
67  //Set the XT1 frequency to UCS
68  UCS_setExternalClockSource(32768, 0);
69 
70  //Configure Pins for I2C (UCB0SCL, UCB0SDA)
71  //Set P2.5 and P2.6 as Module Function Input
72  GPIO_setAsPeripheralModuleFunctionInputPin(
73  GPIO_PORT_P2,
74  GPIO_PIN5 + GPIO_PIN6
75  );
76 
77  EUSCI_B_I2C_initMasterParam param = {0};
78  param.selectClockSource = EUSCI_B_I2C_CLOCKSOURCE_SMCLK;
79  param.i2cClk = UCS_getSMCLK();
80  param.dataRate = EUSCI_B_I2C_SET_DATA_RATE_400KBPS;
81  param.byteCounterThreshold = 0;
82  param.autoSTOPGeneration = EUSCI_B_I2C_NO_AUTO_STOP;
83  EUSCI_B_I2C_initMaster(EUSCI_B0_BASE, &param);
84 
85 
86  //Set Master in receive mode
87  EUSCI_B_I2C_setMode(EUSCI_B0_BASE,
88  EUSCI_B_I2C_TRANSMIT_MODE
89  );
90  //Enable I2C Module to start operations
91  EUSCI_B_I2C_enable(EUSCI_B0_BASE);
92 
93  EUSCI_B_I2C_clearInterrupt(EUSCI_B0_BASE,
94  EUSCI_B_I2C_TRANSMIT_INTERRUPT0 +
95  EUSCI_B_I2C_NAK_INTERRUPT
96  );
97 
98  //Enable master Receive interrupt
99  EUSCI_B_I2C_enableInterrupt(EUSCI_B0_BASE,
100  EUSCI_B_I2C_TRANSMIT_INTERRUPT0 +
101  EUSCI_B_I2C_NAK_INTERRUPT
102  );
103 
104  SlaveFlag = 0;
105  while(1)
106  {
107  //Specify slave address
108  EUSCI_B_I2C_setSlaveAddress(EUSCI_B0_BASE,
110  );
111 
112 
113  TXByteCtr = 1; // Load TX byte counter
114 
115  while (EUSCI_B_I2C_SENDING_STOP == EUSCI_B_I2C_masterIsStopSent
116  (EUSCI_B0_BASE));
117 
118 
119  EUSCI_B_I2C_masterSendStart(EUSCI_B0_BASE);
120 
121  __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
122  // Remain in LPM0 until all data is TX'd
123 
124  // Change Slave address
125  SlaveFlag++;
126  if(SlaveFlag > 3) // Roll over slave address
127  {
128  SlaveFlag = 0;
129  __delay_cycles(1000); // Delay between transmissions
130  }
131  }
132 }
133 
134 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
135 #pragma vector=USCI_B0_VECTOR
136 __interrupt
137 #elif defined(__GNUC__)
138 __attribute__((interrupt(USCI_B0_VECTOR)))
139 #endif
140 void USCIB0_ISR(void)
141 {
142  switch(__even_in_range(UCB0IV, USCI_I2C_UCBIT9IFG))
143  {
144  case USCI_NONE: // No interrupts break;
145  break;
146  case USCI_I2C_UCALIFG: // Arbitration lost
147  break;
148  case USCI_I2C_UCNACKIFG: // NAK received (master only)
149  // Resend START if NAK'd
150  EUSCI_B_I2C_masterSendStart(EUSCI_B0_BASE);
151  break;
152  case USCI_I2C_UCSTTIFG: // START condition detected with own address (slave mode only)
153  break;
154  case USCI_I2C_UCSTPIFG: // STOP condition detected (master & slave mode)
155  break;
156  case USCI_I2C_UCRXIFG3: // RXIFG3
157  break;
158  case USCI_I2C_UCTXIFG3: // TXIFG3
159  break;
160  case USCI_I2C_UCRXIFG2: // RXIFG2
161  break;
162  case USCI_I2C_UCTXIFG2: // TXIFG2
163  break;
164  case USCI_I2C_UCRXIFG1: // RXIFG1
165  break;
166  case USCI_I2C_UCTXIFG1: // TXIFG1
167  break;
168  case USCI_I2C_UCRXIFG0: // RXIFG0
169  break;
170  case USCI_I2C_UCTXIFG0: // TXIFG0
171  // Check TX byte counter
172  if (TXByteCtr)
173  {
174  EUSCI_B_I2C_masterSendMultiByteNext(EUSCI_B0_BASE,
175  TXData[SlaveFlag]);
176  // Decrement TX byte counter
177  TXByteCtr--;
178  }
179  else
180  {
181  EUSCI_B_I2C_masterSendMultiByteStop(EUSCI_B0_BASE);
182  // Exit LPM0
184  }
185  break;
186  case USCI_I2C_UCBCNTIFG: // Byte count limit reached (UCBxTBCNT)
187  break;
188  case USCI_I2C_UCCLTOIFG: // Clock low timeout - clock held low too long
189  break;
190  case USCI_I2C_UCBIT9IFG: // Generated on 9th bit of a transmit (for debugging)
191  break;
192  default:
193  break;
194  }
195 }
196 
MPU_initThreeSegmentsParam param
__bic_SR_register_on_exit(LPM3_bits|GIE)
__delay_cycles(500000)